<template>
  <div>
    <div class="maincss">
      <el-row>
        <el-card>
          <searchBox title="耗材管理" :btn="btn" @func="showBtn" />

          <el-card class="gray-card no-border no-margin-top" shadow="never">
            <el-row class="searchbox">
              <el-col span="3"> 检验项目 </el-col>
              <el-col span="5">
                <el-input
                  placeholder="项目名称、编号"
                  v-model="queryParams.materialId"
                >
                </el-input>
              </el-col>
              <el-col span="5">
                <el-select
                  v-model="queryParams.type"
                  placeholder="选择耗材类型"
                  class=""
                >
                  <el-option
                    v-for="item in type"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-col>
              <el-col span="1">
                <el-button @click="handleLoadAndQuery()">查询</el-button>
              </el-col>
            </el-row>

            <el-table
              v-loading="loading"
              :data="checkList"
              style="width: 100%;"
              ref="singleTable"
            >
              <el-table-column
                label="耗材编号"
                align="center"
                prop="materialId"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="耗材名称"
                align="center"
                prop="materialName"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="耗材类型"
                align="center"
                prop="type"
                :show-overflow-tooltip="true"
              >
                <template slot-scope="scope">
                  <span v-if="scope.row.type === 1">试剂</span>
                  <span v-if="scope.row.type === 2">质控品</span>
                </template>
              </el-table-column>
              <el-table-column
                label="包装规格"
                align="center"
                prop="packAge"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="可检项目"
                align="center"
                prop="project"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="应用科室"
                align="center"
                prop="deptName"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="创建时间"
                align="center"
                prop="createTime"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="生产厂商"
                align="center"
                prop="manufacturer"
                :show-overflow-tooltip="true"
              />
              <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                  <span v-if="scope.row.status === 0">停用</span>
                  <span v-if="scope.row.status === 1">启用</span>
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleOpenEdit(scope.row)"
                    >编辑</el-button
                  >
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleOpenDetail(scope.row)"
                    >详情</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-card>
          <div class="pagination-container">
            <pagination
              v-show="total > 0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="handleLoadAndQuery"
            />
          </div>
        </el-card>
      </el-row>
    </div>

    <el-drawer :visible.sync="addShow" size="70%" @close="handleCloseAdd">
      <addEdit
        @func="showAdd"
        :deviceInfo="deviceInfo"
        :editSaleInfo="editSaleInfo"
        :flag="false"
        ref="mainRef"
      />
    </el-drawer>
  </div>
</template>

<script>
import addEdit from "@/views/consumable/management/components/addEdit";
import detail from "@/views/consumable/management/components/detail";
import { getMaterial, materialList } from "../../../api/business/material";

export default {
  name: "management",
  components: {
    addEdit,
    detail
  },
  data() {
    return {
      btn: {
        management: true,
        add: true,
        reg: false,
        down: false,
        export: false,
        imp: false
      },
      total: 10,
      addShow: false,
      checkList: [],
      loading: false,
      redirect: undefined,
      activeName: "1",
      deviceInfo: {},
      editSaleInfo: {},
      type: [
        {
          value: null,
          label: "选择耗材类型"
        },
        {
          value: 1,
          label: "试剂"
        },
        {
          value: 2,
          label: "质控品"
        }
      ],
      value: "",
      isVisibleAdd: false,
      checkId: "",
      isVisible: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialId: "",
        type: ""
      }
    };
  },
  watch: {},
  created() {
    this.handleLoadAndQuery();
  },
  methods: {
    setCurrent(row) {
      this.$refs.singleTable.setCurrentRow(row);
    },
    handleLoadAndQuery() {
      this.loading = true;
      materialList(this.queryParams).then(response => {
        this.loading = false;
        this.checkList = response.rows;
        this.total = response.total;
      });
    },
    // 新增
    showBtn(data) {
      console.log(data);
      if (data == "add") {
        this.addShow = true;
      }
    },
    showAdd(data) {
      if (!data) {
        this.addShow = false;
        this.handleLoadAndQuery();
      }
    },
    handleOpenAdd() {
      this.isVisibleAdd = true;
    },
    handleCloseAdd() {
      this.isVisibleAdd = false;
      this.handleLoadAndQuery();
    },
    handleOpenDetail(row) {
      this.$router.push({
        path: "/consumable/management/components/detail",
        query: { id: row.id }
      });
    },
    handleOpenEdit(val) {
      this.addShow = true;
      this.$nextTick(() => {
        this.$refs.mainRef.handleData(val);
      });
    }
  }
};
</script>
